package lime.application.dialog;

import java.util.Map;

import javax.inject.Inject;

import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class LoginDialog extends TitleAreaDialog {

	private static final String CREDENTIALS_USERNAME = "credentials.username";
	private static final String CREDENTIALS_PASSWORD = "credentials.password";
	private static final String CREDENTIALS_APPLICATION = "credentials.application";
	private static final String CREDENTIALS_URL = "credentials.url";
	
	// Widgets
	private Text textUsername;
	private Text textPassword;
	private Text textApplication;
	private Text textUrl;

	@Inject
	MApplication application;
	
	public LoginDialog(Shell parent) {
		super(parent);
	}
	
	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setTitle("Credentials");
		setMessage("Please provide the credentials.");
		return contents;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		
		Map<String, String> state = application.getPersistedState();
		String persistedUsername = state.get(CREDENTIALS_USERNAME);
		String persistedPassword = state.get(CREDENTIALS_PASSWORD);
		String persistedApplication = state.get(CREDENTIALS_APPLICATION);
		String persistedUrl = state.get(CREDENTIALS_URL);
		
		Composite area = (Composite) super.createDialogArea(parent);
		
		Composite container = new Composite(area, SWT.NULL);
		container.setLayout(new GridLayout(2, false));
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		new Label(container, SWT.NULL).setText("Username");
		textUsername = new Text(container, SWT.BORDER);
		textUsername.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		if (persistedUsername != null) {
			textUsername.setText(persistedUsername);
		}
		
		new Label(container, SWT.NULL).setText("Password");
		textPassword = new Text(container, SWT.PASSWORD | SWT.BORDER);
		textPassword.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		if (persistedPassword != null) {
			textPassword.setText(persistedPassword);
		}
		
		new Label(container, SWT.NULL).setText("Application");
		textApplication = new Text(container, SWT.BORDER);
		textApplication.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		if (persistedApplication != null) {
			textApplication.setText(persistedApplication);
		}
		
		new Label(container, SWT.NULL).setText("URL");
		textUrl = new Text(container, SWT.BORDER);
		textUrl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		if (persistedUrl != null) {
			textUrl.setText(persistedUrl);
		}
		
		return area;
	}
	
	@Override
	protected Point getInitialSize() {
		return new Point(500, 300);
	}
	
	@Override
	protected void okPressed() {
	
		Map <String, String> persistedState = application.getPersistedState();
		
		persistedState.put(CREDENTIALS_USERNAME, textUsername.getText());
		persistedState.put(CREDENTIALS_PASSWORD, textPassword.getText());
		persistedState.put(CREDENTIALS_APPLICATION, textApplication.getText());
		persistedState.put(CREDENTIALS_URL, textUrl.getText());
		
		super.okPressed();
	}
	
}
